package Com.HupochuanAdmin.Dal.Tab;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.DBManager.DBManager;
import Com.Hupochuan.Vo.MovieType;

public class MovieTypeTab {

	/*
	 * 本类私有对象
	 */
	private DBManager<MovieType> dbManager = new DBManager<MovieType>(MovieType.class);
	
	/*
	 * 单一实例
	 */
	private MovieTypeTab(){}
	private static MovieTypeTab movieTypeTab = new MovieTypeTab();
	public static MovieTypeTab getObject(){
		return movieTypeTab;
	}
	
	/*
	 * 添加数据
	 */
	public int appendData(long movieId,long typeId,float weight){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" insert into MovieType set ");
		sqlBuff.append(" 	movieId=${movieId}, ");
		sqlBuff.append(" 	typeId=${typeId}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	createTime=now(), ");
		sqlBuff.append(" 	updateTime=now() ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		paraHash.put("typeId",typeId);
		paraHash.put("weight",weight);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 更新数据
	 */
	public int updateData(long typeId,float weight,long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" update MovieType set ");
		sqlBuff.append(" 	typeId=${typeId}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	updateTime=now() ");
		sqlBuff.append(" where id=${id} and ( ");
		sqlBuff.append(" 	typeId!=${typeId} ");
		sqlBuff.append(" 	or weight!=${weight} ");
		sqlBuff.append(" ) ");
		
		//sql参数部分
		paraHash.put("typeId",typeId);
		paraHash.put("weight",weight);
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 删除数据
	 */
	public int deleteData(long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieType where id=${id} ");
		
		//sql参数部分
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据电影ID 删除数据
	 */
	public int deleteDataByMovieId(long movieId){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieType where movieId=${movieId} ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 获得电影类型列表
	 */
	public List<MovieType> findMovieTypeList(long movieId){
		//参数部分
		List<MovieType> movieTypeList = new Vector<MovieType>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select MovieType.*, ");
		sqlBuff.append(" 	Type.name as typeName ");
		sqlBuff.append(" from MovieType  ");
		sqlBuff.append(" left join Type on MovieType.typeId=Type.id ");
		sqlBuff.append(" where movieId=${movieId} ");
		sqlBuff.append(" order by ");
		sqlBuff.append(" 	MovieType.weight desc, ");
		sqlBuff.append(" 	Type.weight desc, ");
		sqlBuff.append(" 	Type.id, ");
		sqlBuff.append(" 	MovieType.id ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		
		//执行sql 
		movieTypeList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(),paraHash);
		
		//返回列表
		return movieTypeList;
		
	}
}
